package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MenuMapper;
import com.itheima.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    MenuMapper menuMapper;


    @Override
    public List<Menu> findMenuByUserName(String userName) {
       Integer userId = menuMapper.findUserIdByUserName(userName);
       Integer roleId = menuMapper.findRoleIdByUserId(userId);
       //查询所有的一级菜单
       List<Menu> menuOne =  menuMapper.findMenuByRoleId(roleId);
       if (null==menuOne){
           return null;
       }
       //循环一级菜单查询一级菜单下面的二级
        for (Menu menu : menuOne) {
           List<Menu> menuTwo =  menuMapper.findMenuByParentId(menu.getId());
           menu.setChildren(menuTwo);
        }
        return menuOne;
    }
}
